#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 35;

int n;
int dp[N][N];
int w[N];

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++ ) cin >> w[i];
    
    for (int len = 1; len <= n; len ++ )
        for (int l = 1; l + len - 1 <= n; l ++ )
        {
            int r = l + len - 1;
            for (int k = l; k <= r; k ++ )
            {    
                int left = k == l ? 1 : dp[l][k - 1];
                int right = k == r ? 1 : dp[k + 1][r];
                int score = left * right + w[k];
                if (l == r) score = w[k];
                if (dp[l][r] < score)
                {
                    dp[l][r] = score;
                    // root[l][r] = k;
                }
                dp[l][r] = max(dp[l][r], score);
            }
        }
    
    cout << dp[1][n] << endl;
    return 0;
}
